//
//  HMHomeTableTableViewController.swift
//  weibo
//
//  Created by LCH on 16/3/2.
//  Copyright © 2016年 lch. All rights reserved.
//

import UIKit

private let HMStatusCellID = "HMStatausCellID"

class HMHomeTableTableViewController: HMVisitorViewController {
    
    var statusList: HMStatusListViewModel = HMStatusListViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
     
      print("\(userLogin)")
        
        if !userLogin {
        
            visitorView.setPageInfo(nil, message: nil)
            
            return
        }
     
        setupUI()
        
        
        
    }
   // 按钮点击无法显示hightLighted状态(已修正) 
   // 用字符串来调用方法
    private func setupUI() {
    
    navigationItem.leftBarButtonItem = UIBarButtonItem(imageName:"navigationbar_friendsearch", target: nil, action: nil)
        
    navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "pop")
        
    
      setupTableView()
      loadData()
    
    }
//  动态计算cell的高度
  private func setupTableView() {
    
     tableView.registerClass(HMStatusViewCell.self, forCellReuseIdentifier: HMStatusCellID)
    
    tableView.rowHeight = UITableViewAutomaticDimension
    
    tableView.estimatedRowHeight = 250
    
    tableView.separatorStyle = .None
    
    tableView.backgroundColor = UIColor(white: 239/255, alpha: 1)
    // TODO: tableHeaderView   tableFooterView   tableHeaderFooterView
    tableView.tableFooterView = pullUpView
//    tableView.tableHeaderView = pullUpView
    
    }
    
    private func loadData() {
    
        statusList.loadData(pullUpView.isAnimating(),finish: { (isSuccess:Bool) -> () in
            
            if isSuccess {
                
                self.tableView.reloadData()
            }else {
                
                print("HOME请求数据失败")
                
                
            }
            
            self.endRefresh()
            })
    
       
    }
    private func endRefresh() {
    
    pullUpView.stopAnimating()
    
    }
    
    
    func pop() {
    
    let vc = HMTempViewController()
    navigationController?.pushViewController(vc, animated: true)
    
    }
    
    
    
    private lazy var pullUpView: UIActivityIndicatorView = {
    
     let indicatiorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        indicatiorView.color = UIColor.purpleColor()
//        indicatiorView.startAnimating()
        return indicatiorView
    
    }()
    
}

extension HMHomeTableTableViewController {
    
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
 
        return self.statusList.statusArray?.count ?? 0
        
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HMStatusCellID, forIndexPath: indexPath) as!
            HMStatusViewCell
        
//        cell.textLabel?.text = self.statusList.statusArray![indexPath.row].user?.screen_name
        
        cell.statusViewModel = self.statusList.statusArray![indexPath.row]
        
        return cell
        
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
//        let index = 1
//        print(index)
        
        if indexPath.row == statusList.statusArray!.count - 1 && pullUpView.isAnimating() == false {
        
           pullUpView.startAnimating()
        
//           print("去加载数据")
            self.loadData()
            
            
        }
    }
    

}

